package com.hongdee.crm.average.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.common.util.StringUtil;
import com.hongdee.crm.average.dao.IAverageDao;
import com.hongdee.crm.average.service.ITCrmAverageMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/6/8.
 */
@Service
public class TCrmAverageMaintainCarServiceImpl implements ITCrmAverageMaintainService {
    @Autowired
    private IAverageDao iAverageDao;
    private  String tabName="T_CRM_AVERAGE_MAINTAIN";
    @Autowired
    private IHibernateDao iHibernateDao;

    @Override
        public Map newPage(String start, String limit, Map data) throws Exception {
        String sql = "select * from  T_CRM_AVERAGE_MAINTAIN where 1=1 ";
        List<Object> params = new ArrayList();
        Object year=data.get("year");
        if (!StringUtils.isEmpty(year)&&year!="") {
            sql += "and year= ?";
            params.add(year);
        }
        Object carType=data.get("carType");
        if (!StringUtils.isEmpty(carType)&carType!=""){
            sql +=  "and car_type=?";
            params.add(carType);
        }
        Object plateNum=data.get("plateNum");
        if (!StringUtils.isEmpty(plateNum)&plateNum!=""){
            sql += "and plate_num=?";
            params.add(plateNum);
        }
        Object vin=data.get("vin");
        if (!StringUtils.isEmpty(vin)&vin!=""){
            sql +="and vin=?";
            params.add(vin);
        }
//        Map maintainDate=(HashMap)data.get("maintainDate");
//        if (maintainDate.get("maintain_first") != "") {
//            String startTime = String.valueOf(maintainDate.get("maintain_first"));
//            startTime = startTime.substring(0, 10);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Long startTimeLong = sdf.parse(startTime).getTime();
//            sql +=(" and maintain_first >= ?  ");
//            params.add(startTimeLong);
//        }
//        if (maintainDate.get("maintain_end") != "") {
//            String stopTime = String.valueOf(maintainDate.get("maintain_end"));
//            stopTime = stopTime.substring(0, 10) + " 23:59:59";
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Long stopTimeLong = sdf.parse(stopTime).getTime();
//            sql+= (" and maintain_end <= ?  ");
//            params.add(stopTimeLong);
//        }
//        Object carAge=data.get("carAge");
//        if (!StringUtils.isEmpty(carAge)&carAge!=""){
//            sql +="and car_age=?";
//            params.add(carAge);
//        }
        System.out.print(sql.toString());
        return this.iAverageDao.page(sql, params.toArray(), start, limit);
    }
}
